package com.cheyun.netsalev3.repository

import com.cheyun.netsalev3.bean.MoneyParam
import com.cheyun.netsalev3.bean.NoticeData
import com.cheyun.netsalev3.bean.PersonlistParam
import com.cheyun.netsalev3.bean.home.HomeReportInfo
import com.cheyun.netsalev3.bean.home.HomeTaskInfo
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlin.reflect.KFunction1

class HomeTaskFragment2Repository : BaseRepository() {
    fun getData(stime: String, method: (p: PersonlistParam) -> Unit) {
        var uid = 0
        MySharedPreferences.getLoginData()?.member?.uid?.apply {
            uid = this
        }
//        networkState.postValue(NetworkState.LOADING)
        ApiClient.getInstance().getPersonlist(uid.toString(), stime)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<PersonlistParam>>() {
                    override fun onSuccess(t: BaseResp<PersonlistParam>) {
                        networkState.postValue(NetworkState.LOADED)
                        t.data?.apply {
                            for (itm in this.connect_data) {
                                itm.type = 1
                            }
                            method(this)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })

    }

    var uid = MySharedPreferences.getLoginData()?.member?.uid ?: 0
    /**
     * 获取消息
     */
    fun GetMessage(kFunction1: KFunction1<@ParameterName(name = "item") NoticeData, Unit>) {
        ApiClient.getInstance().noticeDatalist(uid, 1,1)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<NoticeData>>() {
                    override fun onSuccess(t: BaseResp<NoticeData>) {
                        t.data?.apply {
                                kFunction1(this)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }

                })
    }

    fun getMoney( moth: (MoneyParam) -> Unit){
        ApiClient.getInstance().getMoney()
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<MoneyParam>>() {
                    override fun onSuccess(t: BaseResp<MoneyParam>) {
                        moth(t?.data!!)
                    }
                    override fun onError(ex: ApiException) {
                        var a = ""
                    }
                })
    }

    fun getBlist(date:String,series:String, moth: (HomeTaskInfo) -> Unit){
        ApiClient.getInstance().getBlist(date,series)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<HomeTaskInfo>>() {
                    override fun onSuccess(t: BaseResp<HomeTaskInfo>) {
                        moth(t?.data!!)
                    }
                    override fun onError(ex: ApiException) {
                    }
                })
    }
}